import 'package:flutter/material.dart';
import 'package:furaha_mkopo/commonTool/fm_jump_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_report_event_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_time_tool.dart';
import 'package:furaha_mkopo/commonUI/fm_progress_hud.dart';
import 'package:furaha_mkopo/commonUI/fm_toast.dart';
import 'package:furaha_mkopo/page/home/api/fm_home_detail_api.dart';
import 'package:furaha_mkopo/page/home/api/fm_home_ought_api.dart';
import 'package:furaha_mkopo/page/home/entity/fm_home_detail_entity.dart';
import 'package:furaha_mkopo/page/home/entity/fm_home_ought_entity.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';

class FMCommonController extends GetxController{
  Position? position;
  void onRequestNoEmptyListen(BuildContext context,String listen) {
    FMProgressHud.showLoading();
    FMHomeDetailApi api = FMHomeDetailApi(listen);
    api
        .send<FMHomeDetailEntity, FMHomeDetailEntity>(
            decodeType: FMHomeDetailEntity())
        .then((value) {
      FMProgressHud.hideLoading();
      if (value.data == null) {
        FMToast.showText(value.message ?? "Network request error");
        return;
      }
      String tag = value.data?.declared?.hunt ?? "";
      String bidding = value.data?.declared?.bidding ?? "";
      if(tag.isEmpty){
        Navigator.pop(context);
        return;
      }
      FMJumpTool.jumpAuth(context, tag, bidding, listen, true);
    });
  }

  void onRequestGoOught(BuildContext context,String listen) {
    FMProgressHud.showLoading();
    FMHomeDetailApi api = FMHomeDetailApi(listen);
    api
        .send<FMHomeDetailEntity, FMHomeDetailEntity>(
        decodeType: FMHomeDetailEntity())
        .then((value) {
      FMProgressHud.hideLoading();
      if (value.data == null) {
        FMToast.showText(value.message ?? "Network request error");
        return;
      }
      onRequestOught(value.data?.masters);
    });
  }

  void onRequestOught(Masters? masters){
    String adopt = masters?.adopt ??"";
    String attitude = masters?.attitude ??"";
    String affair = masters?.affair ??"";
    String projects = masters?.projects.toString() ??"";
    FMProgressHud.showLoading();
    FMHomeOughtApi oughtApi = FMHomeOughtApi(adopt, attitude, affair, projects);
    oughtApi.send<FMHomeOughtEntity,FMHomeOughtEntity>(decodeType: FMHomeOughtEntity()).then((value){
      FMProgressHud.hideLoading();
      if(value.code == null || value.code != 0){
        FMToast.showText(value.message ?? "Network request error");
        return;
      }
      int endTime = FMTimeTool.currentTime;
      FMReportEventTool.reportRisk("9", position?.latitude, position?.longitude, endTime, endTime);
      FMJumpTool.jumpWebWidget(url: value.data?.bidding??'');
    });
  }
}